package com.zou.jokes;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.common.logging.FLog;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.lzy.widget.AlphaIndicator;
import com.zou.jokes.fragments.BaseFragment;
import com.zou.jokes.fragments.JokeFragment;
import com.zou.jokes.fragments.MyFavoriteFragment;
import com.zou.jokes.fragments.PicsFragment;
import com.zou.jokes.fragments.SettingFragment;
import com.zou.jokes.utils.RxBus;
import com.zou.jokes.utils.UserEvent;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import rx.Subscription;

public class MainActivity extends AppCompatActivity {

    private List<Fragment> fragments;
    private Subscription   rxSubscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OkHttpClient okHttpClient = new OkHttpClient();

//        fresco设置、初始化
        FLog.setMinimumLoggingLevel(FLog.VERBOSE);
        //Fresco.initialize(this);
        DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(this)
                .setBaseDirectoryPath(new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "Moe Studio"))
                .setBaseDirectoryName("simple_joke")
                .setMaxCacheSize(200 * 1024 * 1024)//200MB
                .build();
//        使用okhttp作为文件传输方式
        ImagePipelineConfig imagePipelineConfig = OkHttpImagePipelineConfigFactory.newBuilder(this, okHttpClient)
                .setMainDiskCacheConfig(diskCacheConfig)
                .build();
        Fresco.initialize(this, imagePipelineConfig);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        assert toolbar != null;
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));


        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        assert viewPager != null;
        viewPager.setOffscreenPageLimit(4);
        fragments = new ArrayList<>();
        fragments.add(new JokeFragment());
        fragments.add(new PicsFragment());
//        fragments.add(new BaseFragment());
        fragments.add(new MyFavoriteFragment());
        fragments.add(new SettingFragment());

        MyVPAdapter myVPAdapter = new MyVPAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myVPAdapter);

        AlphaIndicator alphaIndicator = (AlphaIndicator) findViewById(R.id.alphaIndicator);
        assert alphaIndicator != null;
        alphaIndicator.setViewPager(viewPager);

//        RxBus接收数据
        //                        处理异常
        rxSubscription = RxBus.getDefault().toObserverable(UserEvent.class)
                .subscribe(userEvent -> {
//
                    long tag = userEvent.getId();

                }, throwable -> {
//                        处理异常
                });
    }


    class MyVPAdapter extends FragmentPagerAdapter {
        public MyVPAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        取消被订阅避免内存泄漏
        if (!rxSubscription.isUnsubscribed()) {
            rxSubscription.unsubscribe();
        }
    }
}
